package com.photography.wedding.controller;


import com.photography.wedding.pojo.Menus;
import com.photography.wedding.pojo.News;
import com.photography.wedding.pojo.Picture;
import com.photography.wedding.pojo.User;
import com.photography.wedding.service.MenusService;
import com.photography.wedding.service.NewsService;
import com.photography.wedding.service.PhotoService;
import com.photography.wedding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CommunalController {
    @Autowired
    private UserService userService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private MenusService menusService;
    @Autowired
    private PhotoService photoService;

    @RequestMapping(value = "/login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/admin")
    public String admin(HttpServletRequest request){
        try {
            HttpSession session = request.getSession(true);
            session.setAttribute("user_leve", "0");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "admin";
    }

    @RequestMapping(value = "/user")
    public String user(HttpServletRequest request){
        try {
            HttpSession session = request.getSession(true);
            session.setAttribute("user_leve", "2");
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "user";
    }

    @RequestMapping(value = "/adminStrationManagement")
    public String adminStrationManagement(HttpServletRequest request){
        try {
            List<User> userBean = userService.selectAll();
            if (userBean.size() != 0){
                HttpSession session = request.getSession(true);
                session.setAttribute("alluser", userBean);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "adminStrationManagement";
    }

    @RequestMapping(value = "/addUsers")
    public String addUsers(){
        return "addUsers";
    }

    @RequestMapping(value = "/adminUpdateUsers")
    public String adminUpdateUsers(){
        return "adminUpdateUsers";
    }

    @RequestMapping(value = "/personalData")
    public String personalData(){
        return "personalData";
    }

    @RequestMapping(value = "/changePassword")
    public String changePassword(){
        return "changePassword";
    }

    @RequestMapping(value = "/assistance")
    public String assistance(){
        return "assistance";
    }

    @RequestMapping(value = "/adminLatestNews")
    public String adminLatestNews(){
        return "adminLatestNews";
    }

    @RequestMapping(value = "/adminWorksAppreciation")
    public String adminWorksAppreciation(){
        return "adminWorksAppreciation";
    }

    @RequestMapping(value = "/latestNews")
    public String latestNews(HttpServletRequest request){
        try {
            List<News> newsList = newsService.selectNewsAll();
            if (newsList.size() != 0){
                HttpSession session = request.getSession(true);
                session.setAttribute("allnews", newsList);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "latestNews";
    }

    @RequestMapping(value = "/worksAppreciation")
    public String worksAppreciation(HttpServletRequest request){
        try {

            List<Menus> menusList = menusService.selectAdminMenuAll("0");
            List<Picture> pictureList = photoService.selectAdminPictureAll("0");

            for (int j = 0; j < menusList.size(); j++){
                List<Picture> pictureList1 = new ArrayList<>();
                for (int i = 0; i < pictureList.size(); i++){
                    if (menusList.get(j).getModuleName().equals(pictureList.get(i).getModuleName())){
                        pictureList1.add(pictureList.get(i));
                    }
                }

                menusList.get(j).setPictureList(pictureList1);
            }

            if (menusList.size() != 0 ){
                HttpSession session = request.getSession(true);
                session.setAttribute("menuall", menusList);
            }


        }catch (Exception e) {
            e.printStackTrace();
        }
        return "worksAppreciation";
    }

    @RequestMapping(value = "/photoAlbums")
    public String photoAlbums(){
        return "photoAlbums";
    }

    @RequestMapping(value = "/myPictureFile")
    public String myPictureFile(){
        return "myPictureFile";
    }

    @RequestMapping(value = "/managementPhoto")
    public String managementPhoto(){
        return "managementPhoto";
    }

    @RequestMapping(value = "/andminAllLatestNews")
    public String andminAllLatestNews(HttpServletRequest request){
        try {
            List<News> newsList = newsService.selectNewsAll();
            if (newsList.size() != 0){
                HttpSession session = request.getSession(true);
                session.setAttribute("adminallnews", newsList);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "andminAllLatestNews";
    }

    @RequestMapping(value = "/addMenus")
    public String addMenus(){
        return "addMenus";
    }

    @RequestMapping(value = "/addPicture")
    public String addPicture(){
        return "addPicture";
    }

    @RequestMapping(value = "/adminTypePicture")
    public String adminTypePicture(){
        return "adminTypePicture";
    }

    @RequestMapping(value = "/regulatorMyPictureFile")
    public String regulatorMyPictureFile(){
        return "regulatorMyPictureFile";
    }

    @RequestMapping(value = "/myFolder")
    public String myFolder(){
        return "myFolder";
    }

    @RequestMapping(value = "/updateMenus")
    public String updateMenus(){
        return "updateMenus";
    }

    @RequestMapping(value = "/adminUpdateMenus")
    public String adminUpdateMenus(){
        return "adminUpdateMenus";
    }

    @RequestMapping(value = "/advisoryDetails")
    public String advisoryDetails(){
        return "advisoryDetails";
    }

    @RequestMapping(value = "/seeMore")
    public String seeMore(){
        return "seeMore";
    }

    @RequestMapping(value = "/adminUpdateLatestNews")
    public String adminUpdateLatestNews(){
        return "adminUpdateLatestNews";
    }


    @RequestMapping(value = "/anminUpdatePicture")
    public String anminUpdatePicture(){
        return "anminUpdatePicture";
    }


}
